<template>
  <div>
    <el-form
      :model="artForm"
      :rules="artRules"
      ref="artForm"
      label-width="100px"
    >
      <el-form-item label="文章标题" prop="title">
        <el-input
          placeholder="请输入文章标题"
          v-model="artForm.title"
        ></el-input>
      </el-form-item>

      <el-form-item label="文章内容" prop="articleBody">
        <!-- 使用 v-model 进行双向的数据绑定 -->
        <quill-editor
          @change="$refs.artForm.clearValidate('articleBody')"
          v-model="artForm.articleBody"
        ></quill-editor>
      </el-form-item>

      <el-form-item label="视频地址">
        <el-input
          v-model="artForm.videoURL"
          placeholder="请输入视频地址"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button @click="calloff">取消</el-button>
        <el-button @click="submit" type="primary">确认</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { add, update } from '@/api/hmmm/articles.js'
export default {
  name: 'addDialog',
  props: {
    row: {
      type: Object,
      default: () => {}
    },
    isEdit: {
      type: Boolean
    }
  },
  data () {
    return {
      artForm: {
        title: '',
        videoURL: '',
        content: ''
      },
      artRules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: 'blur' },
          { min: 1, max: 30, message: '文章标题的长度为1-30个字符' }
        ],
        articleBody: [
          { required: true, message: '请输入文章内容', trigger: 'blur' }
        ]
      }
    }
  },
  created () {},
  computed: {},
  watch: {
    isEdit: {
      handler (val) {
        if (val) {
          this.artForm = JSON.parse(JSON.stringify(this.row))
        }
      },
      immediate: true,
      deep: true
    }
  },
  methods: {
    //   点击确认
    submit () {
      this.$refs.artForm.validate(async (vali) => {
        // console.log(vali)
        if (vali) {
          this.isEdit ? this.doEdit() : this.doAdd()
        }
      })
    },
    async doAdd () {
      await add(this.artForm)
      // console.log(res, '8585')
      this.$message({
        message: '恭喜您，新增文章成功',
        type: 'success'
      })
      this.artForm = {
        title: '',
        videoURL: '',
        content: ''
      }
      this.$emit('success')
    },
    async doEdit () {
      const res = await update(this.artForm)
      console.log(res, 9999)
      this.$message({
        message: '修改成功',
        type: 'success'
      })
      if (res.data.success) {
        this.$emit('success')
      }
      this.artForm = {
        title: '',
        videoURL: '',
        content: ''
      }
    },
    // 点击取消
    calloff () {
      this.artForm = {
        title: '',
        videoURL: '',
        content: ''
      }
      this.$emit('success')
    }
  }
}
</script>
<style lang="scss" scoped>
// 设置富文本编辑器的默认最小高度
::v-deep .ql-editor {
  min-height: 200px;
}
</style>
